﻿using MachineVision.API.Middlewares;
using MachineVision.API.Models;
using MachineVision.Domain;
using MachineVision.Service;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace MachineVision.API.Controllers
{
    [Route("api/video")]
    [ApiController]
    public class YunMouController : ControllerBase
    {

        private readonly ILogger<YunMouController> _logger;
        private readonly ITokenService _tService;
        private readonly IAppSettings settings;

        public YunMouController(ILogger<YunMouController> logger, ITokenService tService, IAppSettings settings)
        {
            _logger = logger;
            _tService = tService;
            this.settings = settings;

        }

        // GET api/<YunMouController>/5
        [HttpGet("{id}")]
        public async Task<ApiResult> Get(int id)
        {
            //var a = settings.token_url;
            return await Task.Run(() =>
            {
                ApiResult result = new ApiResult();

                Token_YunMou token = _tService.GetTokenFromDB();
                if (token == null || token.ExpirationTime <= DateTime.Now)
                {
                    token = _tService.GetTokenFromURL(settings.token_url, settings.client_id, settings.client_secret);
                    _logger.LogInformation("从云眸获取token");

                        bool v = _tService.SetToken(token.AccessToken, token.ExpirationTime);
                        _logger.LogInformation("token保存数据库-"+ v);

                }
                result.data = _tService.GetVideo(settings.video_url, token.AccessToken, settings.device_serial, id, settings.protocol);

                return result;
            });
        }


        [HttpGet("[action]/{id}")]
        public async Task<ApiResult> GetUIKit(int id)
        {
            //var a = settings.token_url;
            return await Task.Run(() =>
            {
                ApiResult result = new ApiResult();

                Token_YunMou token = _tService.GetTokenFromDB();
                if (token == null || token.ExpirationTime <= DateTime.Now)
                {
                    token = _tService.GetTokenFromURL(settings.token_url, settings.client_id, settings.client_secret);
                    _logger.LogInformation("从云眸获取token");

                    bool v = _tService.SetToken(token.AccessToken, token.ExpirationTime);
                    _logger.LogInformation("token保存数据库-" + v);

                }
                result.data = _tService.GetUIKit(settings.uikit_url, token.AccessToken, settings.device_serial, id);

                return result;
            });
        }

        [HttpGet("videoHistory")]
        public async Task<ApiResult> GetHistory(int type,string start, string end )
        {
            //var a = settings.token_url;
            return await Task.Run(() =>
            {
                ApiResult result = new ApiResult();

                Token_YunMou token = _tService.GetTokenFromDB();
                if (token == null || token.ExpirationTime <= DateTime.Now)
                {
                    token = _tService.GetTokenFromURL(settings.token_url, settings.client_id, settings.client_secret);
                    _logger.LogInformation("从云眸获取token");

                    bool v = _tService.SetToken(token.AccessToken, token.ExpirationTime);
                    _logger.LogInformation("token保存数据库-" + v);

                }
                result.data = _tService.GetVideoHistory(settings.video_url, token.AccessToken, settings.device_serial, 1, settings.protocol,type, start, end);

                return result;
            });
        }

    }
}
